﻿using System;
using System.Text;

namespace Datagility.Shpt.SPDocMirror.Service
{
    /// <summary>
    /// The type of change that generated a log entry.
    /// </summary>
    public enum AuditEventType
    {
        Checkin,
        Delete,
        Move,
        Undelete,
        Update
    }

    /// <summary>
    /// Represents an entry in the SharePoint audit log describing a change or update to a document
    ///  in a document library.
    /// </summary>
    public class AuditEntry
    {
        private string docLocation;
        private string docFolder;
        private string docFileName;
        private AuditEventType evnt;
        private string eventData;
        private Guid itemId;
        private DateTime occurred;
        private Guid listId;
        private Guid webId;
        private Guid siteId;
        private int userId;

        /// <summary>
        /// Creates a string representation of all of the properties of an AuditEntry object.
        ///  Useful for logging/debugging.
        /// </summary>
        /// <returns>E.g.
        ///AuditEntry
        ///Type: Update
        ///Name: 
        ///Data: 
        ///Occurred: 2010/10/05 03:58:09
        ///ItemId: cb3bb267-24b4-4884-a68f-490fac1634cf
        ///SiteId: f9eaf677-979d-4bc7-9d30-a2e0b7247c88
        ///UserId: 1073741823
        ///Location: Shared Documents/TestWordDoc.docx
        ///Folder: Shared Documents
        ///File Name: TestWordDoc.docx</returns>
        public override string ToString()
        {
            //This could be XML but I haven't found a use for it yet
            StringBuilder bld = new StringBuilder();

            bld.Append("AuditEntry\n");
            bld.Append(String.Format("Type: {0}\n", evnt.ToString()));
            bld.Append(String.Format("Data: {0}\n", eventData));
            bld.Append(String.Format("Occurred: {0}\n", occurred.ToString("yyyy/MM/dd hh:mm:ss")));
            bld.Append(String.Format("ItemId: {0}\n", itemId.ToString()));
            bld.Append(String.Format("SiteId: {0}\n", siteId.ToString()));
            bld.Append(String.Format("UserId: {0}\n", userId.ToString()));
            bld.Append(String.Format("Location: {0}\n", docLocation));
            bld.Append(String.Format("Folder: {0}\n", docFolder));
            bld.Append(String.Format("File Name: {0}\n", docFileName));
            return bld.ToString();
        }

        /// <summary>
        /// The location within SharePoint of the changed document, as read from the Audit Log
        /// E.g. <example>subsite1/Shared Documents/folder1/TestWordDoc.docx</example>
        /// </summary>
        public string DocLocation
        {
            get
            {
                return docLocation;
            }
            set
            {
                //Parse the value to get the DocFolder and DocFileName property
                // values
                docLocation = value;
                string[] parts = docLocation.Split(Convert.ToChar(@"/"));

                //Make the last part of the string the filename
                docFileName = parts[parts.Length - 1];

                //Everything else is the folder
                docFolder = docLocation.Replace("/" + docFileName, @"");
            }
        }

        /// <summary>
        /// The folder path of the changed document, based on the DocLocation property.
        /// E.g. subsite1/Shared Documents/folder1
        /// </summary>
        public string DocFolder
        {
            get
            {
                return docFolder;
            }
        }

        /// <summary>
        /// The file name of the changed document, based on the DocLocation property.
        /// E.g. TestWordDoc.docx
        /// </summary>
        public string DocFileName
        {
            get
            {
                return docFileName;
            }
        }

        /// <summary>
        /// The type of change that that generated the log entry.
        ///  See <see cref="Datagility.Shpt.DocMirror.Lib.AuditEventType"/>
        /// </summary>
        public AuditEventType Event
        {
            get
            {
                return evnt;
            }
            set
            {
                evnt = value;
            }
        }

        /// <summary>
        /// Additional XML data describing the change to the document.
        /// E.g. &lt;Version&gt;&lt;AllVersions/&gt;&lt;/Version&gt;&lt;Recycle&gt;1&lt;Recycle&gt;
        /// </summary>
        public string EventData
        {
            get
            {
                return eventData;
            }
            set
            {
                eventData = value;
            }
        }

        /// <summary>
        /// The Id of the changed document.
        /// </summary>
        public Guid ItemId
        {
            get
            {
                return itemId;
            }
            set
            {
                itemId = value;
            }
        }

        /// <summary>
        /// The DateTime of the change to the document.
        /// </summary>
        public DateTime Occurred
        {
            get
            {
                return occurred;
            }
            set
            {
                occurred = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public Guid ListId
        {
            get 
            { 
                return listId; 
            }
            set 
            { 
                listId = value; 
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public Guid WebId
        {
            get 
            { 
                return webId; 
            }
            set 
            { 
                webId = value; 
            }
        }

        /// <summary>
        /// The Id of the SharePoint Site at which the change occurred.
        /// </summary>
        public Guid SiteId
        {
            get
            {
                return siteId;
            }
            set
            {
                siteId = value;
            }
        }

        /// <summary>
        /// The Id of the user that generated the change to the document.
        /// </summary>
        public int UserId
        {
            get
            {
                return userId;
            }
            set
            {
                userId = value;
            }
        }
    }
}
